package zisu.netty.practice.NIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2020/6/30 14:36
 */
public class NIOFileChannel02 {
    public static void main(String[] args) throws IOException {
        //创建文件的输入流
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        //通过fileInputStream 获取对应的FileChannel对象。 FileChannel实际类型FileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();

        //创建 缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

        //读通道的数据 然后写入到 byteBuffer
        fileChannel.read(byteBuffer);

        //将byteBuffer中的 字节数据 转成 字符串   (String 会自动把byte 数组 转成字符串)
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }
}
